package com.metel.mservice.common.utils.cookie;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class GenerateKey {

	public static byte[] decryptMode(byte[] aKey, byte[] aIV, byte[] aBody) {
		try {
			Key key = new SecretKeySpec(aKey, "AES");
			String transformation = "AES/CBC/PKCS5Padding";
			IvParameterSpec ivParameterSpec = new IvParameterSpec(aIV);

			Cipher cipher = Cipher.getInstance(transformation);
			cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);

			return cipher.doFinal(aBody);// Body의 암호화
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * SSM서버로 보낼 메시지를 암호화 한다.
	 * 
	 * @param aKey
	 * @param aIV
	 * @param aBody
	 * @return
	 * @return byte[]
	 * @exception
	 * @remark N/A
	 * @version 1.0
	 * @author gray 2011. 8. 9. Initial Revision
	 */
	public static byte[] encryptMode(byte[] aKey, byte[] aIV, byte[] aBody) {
		try {
			Key key = new SecretKeySpec(aKey, "AES");// <p>주어진 데이터로, 해당 알고리즘에 사용할 비밀키(SecretKey)를 생성한다.</p>
			String transformation = "AES/CBC/PKCS5Padding";
			IvParameterSpec ivParameterSpec = new IvParameterSpec(aIV);
			Cipher cipher = Cipher.getInstance(transformation);
			cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
			return cipher.doFinal(aBody);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
